// VBConversions Note: VB project level imports
using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using Microsoft.VisualBasic;
using System.Data;
using System.Collections.Generic;
using Microsoft.VisualBasic.Compatibility;
using System.Linq;
// End of VB project level imports

using System.Runtime.InteropServices;

namespace 喷码数据生成工具
{
	public partial class frmCheckData
	{
		public frmCheckData()
		{
			InitializeComponent();
		}
		CommFunction.CheckMode SelectCheck;
		bool CanClearText = false;
		//设置键盘焦点
		[DllImport("user32",EntryPoint="SetFocus", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
		private static extern int SetFocus(IntPtr hWnd);
		
		public void CheckData_Load(System.Object sender, System.EventArgs e)
		{
			Cbo_CheckMode.Items.Clear();
			if (CommFunction.CheckData != null)
			{
				for (var i = 0; i <= CommFunction.CheckData.Count - 1; i++)
				{
					Cbo_CheckMode.Items.Add(CommFunction.CheckData[System.Convert.ToInt32(i)].CheckName);
				}
			}
			if (Cbo_CheckMode.Items.Count > 0)
			{
				Cbo_CheckMode.SelectedIndex = 0;
			}
			CommFunction.ChangeButtonColor(Btn_OK);
		}
		
		public void LB_CheckDescription_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			e.Handled = true;
		}
		
		public void Cbo_CheckMode_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			SelectCheck = getCheckFunName(System.Convert.ToString(Cbo_CheckMode.Items[Cbo_CheckMode.SelectedIndex]));
			LB_CheckDescription.Text = SelectCheck.CheckDescription;
		}
		private CommFunction.CheckMode getCheckFunName(string CheckName)
		{
			CommFunction.CheckMode result = null;
			if (CommFunction.CheckData != null)
			{
				if (CommFunction.CheckData.Count > 0)
				{
					for (var i = 0; i <= CommFunction.CheckData.Count - 1; i++)
					{
						if (Strings.UCase(System.Convert.ToString(CommFunction.CheckData[System.Convert.ToInt32(i)].CheckName)) == CheckName.ToUpper())
						{
							result = CommFunction.CheckData[System.Convert.ToInt32(i)];
						}
						break;
					}
					return result;
				}
				else
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}
		
		public void Btn_OK_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				string CheckString = TB_InputData.Text;
				string tmp = "";
				int a = SelectCheck.CheckLength;
				string OldCheck = "";
				string NewCheck = "";
				if (string.IsNullOrEmpty(CheckString))
				{
					return ;
				}
				if (a <= 0 || SelectCheck.CheckName.ToUpper() == "nocheck".ToUpper())
				{
					Interaction.MsgBox("请选择校验方式！", MsgBoxStyle.Exclamation, "提示");
				}
				return ;
//				tmp = CheckString.Substring(0, CheckString.Length - a);
//				OldCheck = CheckString.Substring(CheckString.Length - a, a);
//				NewCheck = Form1.Default.getCheckNumber(SelectCheck, tmp);
//				if (OldCheck == NewCheck)
//				{
//					Interaction.MsgBox("校验码计算正确！", (int) MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "提示");
//					}
//					else
//					{
//						Interaction.MsgBox("错误的校验码，正确的校验码应为：" + NewCheck, MsgBoxStyle.Critical, "校验失败");
//						}
					}
					catch (Exception ex)
					{
						Interaction.MsgBox("校验码计算失败：" + ex.Message, MsgBoxStyle.Critical, "错误");
					}
					SetFocus(TB_InputData.Handle.ToInt32());
				}
				
				public void TB_InputData_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
				{
					if (CanClearText == true)
					{
						TB_InputData.Text = "";
						CanClearText = false;
					}
					if (Strings.Asc(e.KeyChar) == 13)
					{
						Btn_OK_Click(sender, e);
						CanClearText = true;
					}
				}
			}
		}
